package apigateway.project.core.predicate.PathRoutePredicate;


import apigateway.project.commen.config.Rule;
import apigateway.project.commen.enums.Code;
import apigateway.project.commen.exception.ResponseException;
import apigateway.project.commen.utils.pathPattern;
import apigateway.project.core.context.GlobalContext;
import apigateway.project.core.predicate.Predicate;
import apigateway.project.core.request.GlobalRequest;


import java.util.Map;

import static apigateway.project.commen.constant.PredicateConst.PATH_ROUTE_PREDICATE_ID;


public class PathRoutePredicate implements Predicate {

    private static final String PredicateId = PATH_ROUTE_PREDICATE_ID;

    @Override
    public void doPredicate(GlobalRequest request, Rule rule) {
        //请求路径
        String path = request.getPath();
        Map<String, String> predicateConfigs = rule.getPredicates();
        predicateConfigs.forEach((predicateId, predicateConfig) -> {
            if(PATH_ROUTE_PREDICATE_ID.equals(predicateId)){
                if(!pathPattern.pattern(path, predicateConfig)){
                    throw new ResponseException(Code.PATH_NO_MATCHED);
                }
            }
        });
    }
}
